package com.huping.utils;

import java.util.*;

public class hupingAlgorithm {

    public static int getScoreHuping(int[] hupingArr) {
        bubbleSort(hupingArr);
        double arrLength = hupingArr.length;

        if (hupingArr.length < 4)//数量过少直接取平均
            return (int) calculateAverage(hupingArr);


        //按比例去掉最高最低分
        double trash = Math.ceil(arrLength * 0.05);
        int[] newArray = Arrays.copyOfRange(hupingArr, (int) trash, hupingArr.length - (int) trash);
        if (newArray.length < 8)//互评份数少的时候去掉最小最大，然后剩下的求平均来当作互评总分。
            return (int) calculateAverage(newArray);


        double average = calculateAverage(newArray);
        double standardDeviation = calculateStandardDeviation(newArray);
        int newIndex1 = 0;
        int newIndex2 = newArray.length - 1;
        for (int i = 0; i < newArray.length; i++) {
            if (newArray[i] >= average - standardDeviation) {
                newIndex1 = i;
                break;
            }
        }
        for (int i = newArray.length - 1; i >= 0; i--) {

            if (newArray[i] <= average + standardDeviation) {
                newIndex2 = i;
                break;
            }
        }
        int[] newArray2 = Arrays.copyOfRange(newArray, newIndex1, newIndex2);
        return (int) Math.ceil(calculateAverage(newArray2));

    }


    public static List<Map<String, Object>> getRankList(List<Map<String, Object>> score) {

        int n = score.size();

        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                if ((int) score.get(j).get("scoreTeacher") > (int) score.get(j + 1).get("scoreTeacher")) {
                    Map<String,Object> temp =  score.get(j);
                    score.set(j,score.get(j+1));
                    score.set(j+1,temp);
                }
            }
        }

        for (int i=0;i<score.size();i++ ){

            if(i<n/3){
                score.get(i).put("rankValue","0");
            } else if(i<2*n/3){
                score.get(i).put("rankValue","1");
            }else {
                score.get(i).put("rankValue","2");
            }
        }
        return score;
    }



    // 计算标准差的方法
    private static double calculateStandardDeviation(int[] data) {
        // 步骤1：计算平均值
        double mean = calculateAverage(data);
        // 步骤2和3：计算差异的平方并计算平方差的平均值
        double sumSquaredDifferences = 0;
        for (double value : data) {
            double difference = value - mean;
            sumSquaredDifferences += difference * difference;
        }
        double meanSquaredDifference = sumSquaredDifferences / data.length;
        // 步骤4：计算平均平方差的平方根，即为标准差
        return Math.sqrt(meanSquaredDifference);
    }

    // 计算数组的平均值的方法
    private static double calculateAverage(int[] array) {
        int sum = 0;
        // 遍历数组，累加元素的值
        for (int num : array) {
            sum += num;
        }
        // 计算平均值
        // 如果数组长度为0，可以考虑返回一个特殊值或抛出异常，这里简单地返回0
        return array.length > 0 ? (double) sum / array.length : 0;
    }

    //冒泡排序
    private static void bubbleSort(int[] array) {
        int n = array.length;

        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }


}
